package com.liao.controller;

import com.liao.common.R;
import com.liao.dto.LoginForm;
import com.liao.po.Goodstype;
import com.liao.po.User;
import com.liao.service.UserService;
import com.liao.utils.PageUtils;
import com.liao.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(value = "用户信息管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;



    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R<User> login(@RequestBody LoginForm loginForm, HttpServletResponse response, HttpServletRequest request, HttpSession session,@CookieValue("CaptchaCode") String captchaCode) {


//        if(!session.getAttribute("kaptcha").equals(loginForm.getCode())){
//            return R.error("验证码错误");
//        }

        String captchaValue =  redisTemplate.opsForValue().get(captchaCode);

        if (captchaValue !=null&& captchaValue.equalsIgnoreCase(loginForm.getCode())){

            redisTemplate.delete(captchaCode);
        }else{
            return R.error("验证码错误");
        }



        //1.从数据库中查询用户名对应的用户的对象
        User userbyDB = userService.findById(loginForm.getUsername(), loginForm.getPassword());
        if (userbyDB == null) {
            //2.判断用户是否为空 为空返回数据
            return R.error("用户名或密码错误");
        }


        if(userbyDB !=null){
//            String token = TokenUtil.sign(userbyDB);
//            userbyDB.setToken(token);
            return R.success(userbyDB);
        }else {
            return R.error("登录失败");
        }
    }


    /**
     * 根据参数获取用户列表
     */
    @GetMapping("/list")
    @ApiOperation("根据参数获取用户列表")
    //@RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPageByCodition(params);
        return R.success(page);
    }

    /**
     * 保存或修改
     */
    @PostMapping("/save")
    public R saveOrUpdate(@RequestBody User user){

        user.setEditTime(LocalDateTime.now());
        userService.saveOrUpdate(user);
        return R.success("保存成功");
    }

    /**
     * 保存或修改
     */
    @PostMapping("/updatepassword")
    public R updatePassword(@RequestBody User user){

        user.setEditTime(LocalDateTime.now());
        userService.updateById(user);
        return R.success("保存成功");
    }

    /**
     * 保存或修改
     */
    @PostMapping("/updateIsLock")
    public R updateIsLock(@RequestBody User user){

        user.setEditTime(LocalDateTime.now());
        userService.updateById(user);
        return R.success("修改成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] userIds){
        userService.removeByIds(Arrays.asList(userIds));

        return R.success("删除成功");
    }





}
